﻿using QRCoder;
using System.Drawing;
using System.Drawing.Imaging;

namespace Lg.Core.ImageHelper
{
    public class QrCode
    {
        #region 获取URL生成的二维码
        public static byte[] GetQrCode(string url, int size = 100)
        {
            byte[] bytes = BitmapByteQRCodeHelper.GetQRCode(url, QRCodeGenerator.ECCLevel.H, 20);
            MemoryStream ms = new MemoryStream(bytes);
            Bitmap bitmap = new Bitmap(Image.FromStream(ms), size, size);
            ms = new MemoryStream();
            bitmap.Save(ms, ImageFormat.Png);
            byte[] res = ms.ToArray();
            ms.Close();
            ms.Dispose();
            return res;
        }
        public static string GetBase64QrCodeImgData(string url, int size = 100)
        {
            byte[] byts = GetQrCode(url, size);
            return $"data:image/png;base64,{Convert.ToBase64String(byts)}";
        }
        #endregion
    }
}
